import * as types from './mutations-types'
import {playMode} from 'common/js/config'
import {shuffle} from 'common/js/util'
import {saveSearch,delSearch,delAllsearch,savePlay,addFavorite,removeFavorite} from 'common/js/cache'


function findIndex(list, song) {
    return list.findIndex((item) => {
      return item.id === song.id
    })
}

export const selectPlay=function({commit,state},{list,index}) {
    commit(types.SET_PLAYING_LIST,list)
    commit(types.SET_FULL_SCREEN,true)
    commit(types.SET_PLAYING,true)
    commit(types.SET_CURRENT_INDEX,index)
    commit(types.SET_SEQUENCE_LIST,list)

}
export const randomPlay=function({commit,state},{list}) {
    const randomList=shuffle(list)
    commit(types.SET_PLAYING_LIST,randomList)
    commit(types.SET_PLAYING_MODE,playMode.random)
    commit(types.SET_PLAYING,true)
    commit(types.SET_CURRENT_INDEX,0)
    commit(types.SET_FULL_SCREEN,true)
    commit(types.SET_SEQUENCE_LIST,list)
}

export const insertSong =function({commit,state},song) {
    let playList= state.playList.slice()
    let currentIndex=state.currentIndex
    let sequenceList=state.sequenceList.slice()

    //当前歌曲
    let currentSong=playList[currentIndex]
    //插入的歌曲song 是否在当前播放列表中，并返回其索引
    let fpindex= findIndex(playList, song)

    //因为是插入歌曲，所以索引 + 1
    currentIndex++
    //插入这首歌到当前索引位置
    playList.splice(currentIndex,0,song)
    //若列表已包含此歌
    if (fpindex > -1) {
        //如果当前插入的序号大于列表中的序号
        if (currentIndex > fpindex) {
            playList.splice(fpindex,1)
            currentIndex--
        } else {
            playList.splice(fpindex + 1, 1)
        }
    }

    let currentSIndex= findIndex(sequenceList, currentSong)+ 1

    let fsIndex=findIndex(sequenceList, song)
    sequenceList.splice(currentSIndex,0,song)
    if(fsIndex > -1){
        if(currentSIndex> fsIndex) {
            sequenceList.splice(fsIndex, 1)
        } else {
            sequenceList.splice(fsIndex +1 ,1)
        }
    }

    commit(types.SET_SEQUENCE_LIST,sequenceList)
    commit(types.SET_CURRENT_INDEX,currentIndex)
    commit(types.SET_PLAYING_LIST,playList)
    commit(types.SET_FULL_SCREEN,true)
    commit(types.SET_PLAYING,true)



}

//保存搜索历史
export const saveSearchHistory= function({ commit}, query) {
    commit(types.SET_SEARCH_HISTORY,saveSearch(query))
}

// 删除点击的搜索历史
export const delOneSearchHistory= function ({commit},query) {
    commit(types.SET_SEARCH_HISTORY,delSearch(query))
}

// 删除所有搜索历史
export const delAllSearchHistory=function ({commit}) {
    commit(types.SET_SEARCH_HISTORY,delAllsearch())
}

// 删除点击的歌曲
export const delSong=function ({commit,state},song) {
    let playList= state.playList.slice()
    let currentIndex=state.currentIndex
    let sequenceList=state.sequenceList.slice()

    let pIndex=findIndex(playList,song)
    playList.splice(pIndex,1)

    let sIndex=findIndex(sequenceList,song)
    sequenceList.splice(sIndex,1)

    if( currentIndex > pIndex || currentIndex===playList.length){
        currentIndex--
    }

    commit(types.SET_CURRENT_INDEX,currentIndex)
    commit(types.SET_PLAYING_LIST,playList)
    commit(types.SET_SEQUENCE_LIST,sequenceList)


    const playingState= playList.length > 0
    commit(types.SET_PLAYING,playingState)


}

// 删除全部歌曲
export const delAllSong= function ({commit}) {
    commit(types.SET_CURRENT_INDEX, -1)
    commit(types.SET_PLAYING_LIST, [])
    commit(types.SET_SEQUENCE_LIST, [])
    commit(types.SET_PLAYING,false)
}

// 保存播放记录
export const savePlayHistory= function ({commit,sate},song) {
    commit(types.SET_PLAY_HISTORY,savePlay(song))
}

// 收藏歌曲

export const saveFavorite=function ({commit},song) {
    commit(types.SET_FAVORITE_SONG,addFavorite(song))
}

// 移除歌曲
export const delFavorite=function ({commit},song) {
    commit(types.SET_FAVORITE_SONG,removeFavorite(song))
}